package org.zhouhe.mybatis.page;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class PageInfo {
	private boolean autoFindTotalCount = true;//查询时，要求自动获取记录总数
	private Integer offset;//起始行
	private int totalRecordCount;//总记录数
	private int totalPageCount;//总页数
	private int pageSize = 50;//每页显示的最大记录数
	private int[] pages;//页面上要显示的页连接（包括当前页）
	private int showPage = 1;//当前页
	private int prevPage = 1;//上一页
	private int nextPage = 1;//下一页
	private int pageGroupSize = 12;//界面显示的“分页连接”数量（推荐使用奇数）
	private int prevGroup = 1;
	private int nextGroup = 1;
	private int showGroup = 1;
	private int count;
	private int posStart;
	private Map<Integer, String> pageGroups = new LinkedHashMap<Integer, String>();//用于下拉列表，Key：存放对应组的第一个showPage值，Value：显示内容。（例：key=1,value="1-10页";  key=11,value="11-20页"; key=21,value="21-30页"）。

	private List<?> pageData;

	public class Sql{
		private String select;
		private String from;
		private List<Object> paramList;
		public Sql(String select, String from, List<Object> paramList){
			this.select = select;
			this.from = from;
			this.paramList = paramList;
		}
		public String getSql4Count() {
//			return "select count(*) " + from;
			return "select count (*) from ("+ this.getSql4Data() +")";
		}

		public String getSql4Data() {
			return select + " " + from;
		}
		public void setParamList(List<Object> paramList) {
			this.paramList = paramList;
		}
		public List<Object> getParamList() {
			return paramList;
		}
	}
	public class Hql{
		private String select;
		private String from;
		private List<Object> paramList;
		public Hql(String select, String from, List<Object> paramList){
			this.select = select;
			this.from = from;
			this.paramList = paramList;
		}
		public String getHql4Count() {
//			return "select count(*) " + from + "";
			return "select count(*) " + from + " group by a";
//			return "select count(*) from (  from Article) ";
		}
		public String getHql4Data() {
			return select + " " + from;
		}
		public void setParamList(List<Object> paramList) {
			this.paramList = paramList;
		}
		public List<Object> getParamList() {
			return paramList;
		}
	}
	
	public void setTotalRecordCount(int totalRecordCount) {
		if(totalRecordCount==0)return;
		this.totalRecordCount = totalRecordCount;
		this.totalPageCount = this.totalRecordCount/this.pageSize + (this.totalRecordCount%this.pageSize>0?1:0);
		this.prevPage = this.showPage>1 ? this.showPage-1 : 0;
		this.nextPage = this.showPage<this.totalPageCount ? this.showPage+1 : 0;
		this.pages = new int[0];
		//pageGroups:
		int groupQty = this.totalPageCount/this.pageGroupSize + (this.totalPageCount%this.pageGroupSize>0?1:0);
		for(int i=1; i<= groupQty; i++){
			int groupStart = (i-1)*this.pageGroupSize + 1;
			int groupEnd = groupStart + this.pageGroupSize - 1;
			if(groupEnd > this.totalPageCount){
				groupEnd = this.totalPageCount;
			}
			this.pageGroups.put(groupStart, groupStart +" 到 "+ groupEnd +"页");
		}
		this.showGroup = this.showPage/this.pageGroupSize + ((this.showPage%this.pageGroupSize)>0?1:0);
		this.prevGroup = this.showGroup > 1 ? this.showGroup-1 : 1;
		this.nextGroup = this.showGroup < this.pageGroups.size() ? this.showGroup+1 : this.pageGroups.size();
		//pages:
		int pageStart = this.showPage - ((this.showPage%this.pageGroupSize) == 0 ? this.pageGroupSize : (this.showPage%this.pageGroupSize) )+1;
		int pageEnd = pageStart + this.pageGroupSize - 1;
		if(pageEnd > this.totalPageCount){
			pageEnd = this.totalPageCount;
		}
		this.pages = new int[pageEnd - pageStart + 1];
		for(int i=0; i<this.pages.length; i++){
			this.pages[i] = pageStart + i;
		}
		this.prevGroup = pages[0] - this.pageGroupSize;
		this.prevGroup = this.prevGroup >= 1 ? this.prevGroup : 0;
		this.nextGroup = pages[pages.length-1]+1;
		this.nextGroup = this.nextGroup <= this.totalPageCount ? this.nextGroup : 0;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int[] getPages() {
		return pages;
	}

	public void setPages(int[] pages) {
		this.pages = pages;
	}

	public int getShowPage() {
		return showPage;
	}

	public void setShowPage(int showPage) {
		if(showPage<1){
			showPage = 1;
		}
		this.showPage = showPage;
	}

	public int getPrevPage() {
		return prevPage;
	}

	public void setPrevPage(int prevPage) {
		this.prevPage = prevPage;
	}

	public int getNextPage() {
		return nextPage;
	}

	public void setNextPage(int nextPage) {
		this.nextPage = nextPage;
	}

	public List<?> getPageData() {
		return pageData;
	}

	public void setPageData(List<?> pageData) {
		this.pageData = pageData;
	}

	public int getTotalRecordCount() {
		return totalRecordCount;
	}
	public void setTotlaPageCount(int totalPageCount) {
		this.totalPageCount = totalPageCount;
	}
	public int getTotalPageCount() {
		return totalPageCount;
	}
	public void setPageGroupSize(int pageGroupSize) {
		this.pageGroupSize = pageGroupSize;
	}
	public int getPageGroupSize() {
		return pageGroupSize;
	}
	public void setPageGroups(Map<Integer, String> pageGroups) {
		this.pageGroups = pageGroups;
	}
	public Map<Integer, String> getPageGroups() {
		return pageGroups;
	}
	public void setNextGroup(int nextGroup) {
		this.nextGroup = nextGroup;
	}
	public int getNextGroup() {
		return nextGroup;
	}
	public void setPrevGroup(int prevGroup) {
		this.prevGroup = prevGroup;
	}
	public int getPrevGroup() {
		return prevGroup;
	}
	public void setShowGroup(int showGroup) {
		if(showGroup<1){
			showGroup = 1;
		}
		this.showGroup = showGroup;
	}
	public int getShowGroup() {
		return showGroup;
	}

	public void setOffset(Integer offset) {
		this.offset = offset;
	}

	public Integer getOffset() {
		Integer rs = this.offset;
		if(rs == null){
			rs = this.pageSize * (this.showPage -1);
		}
		return rs;
	}

	public void setCount(int count) {
		this.count = count;
		this.pageSize = count;
	}

	public int getCount() {
		return count;
	}

	public void setPosStart(int posStart) {
		this.posStart = posStart;
		this.offset = posStart;
	}

	public int getPosStart() {
		return posStart;
	}

	public boolean isAutoFindTotalCount() {
		return autoFindTotalCount;
	}

	public void setAutoFindTotalCount(boolean autoFindTotalCount) {
		this.autoFindTotalCount = autoFindTotalCount;
	}
}
